package com.yeyks.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.account.dal.domain.AccountSetout;
import com.yeyks.account.dal.dao.AccountSetoutMapper;
import com.yeyks.account.service.AccountSetoutService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeyks.common.appcommon.param.account.AddAccountSetoutParam;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.exception.OftenFiledException;
import com.yeyks.common.utils.apputil.AesEncoder;
import com.yeyks.common.utils.apputil.redis.RedisService;
import com.yeyks.common.utils.apputil.rediskey.RedisKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 * <p>
 * 账户设备表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-06-25
 */
@Service
@Slf4j
public class AccountSetoutServiceImpl extends ServiceImpl<AccountSetoutMapper, AccountSetout> implements AccountSetoutService {

    @Autowired
    private AccountSetoutMapper accountSetoutMapper;

    @Autowired
    private RedisService redisService;


    @Override
    public CommonResult addAccountSetout(AddAccountSetoutParam addAccountSetoutParam) {
        try {
            //解密
            String accountId = null;
            accountId = AesEncoder.getInstance().decrypt(addAccountSetoutParam.getSign());
            if (StringUtils.isEmpty(accountId)) {
                return CommonResult.failed("传输密文错误,请重新输入");
            }
            if (null != addAccountSetoutParam.getCid() && !"".equals(addAccountSetoutParam.getCid())) {
                AccountSetout setOut = new AccountSetout();
                setOut.setAccountId(Integer.valueOf(accountId));
                setOut.setCId(addAccountSetoutParam.getCid());
                List<AccountSetout> accountSetouts = accountSetoutMapper.selectList(new QueryWrapper<AccountSetout>()
                        .eq(AccountSetout.C_ID, setOut.getCId()));
                if(!CollectionUtils.isEmpty(accountSetouts)){
                    //去除已经存在的设备ID
                    accountSetoutMapper.deleteByCid(addAccountSetoutParam.getCid());
                }
                log.info("新增的设备ID" + addAccountSetoutParam.getCid());
                int count = accountSetoutMapper.insert(setOut);
                if (count > 0) {
                    return CommonResult.success(null, "账户添加设备成功");
                }
            }
            return CommonResult.failed("添加设备失败，请检查信息");
        } catch (Exception e) {
            throw new OftenFiledException(e.getMessage());
        }
    }

    @Override
    public CommonResult delAccountSetout(String sign, String cid) {

        redisService.set(RedisKey.FAILED_DEL_SET_OUT +":"+cid, sign);
        //解密
        String accountId = AesEncoder.getInstance().decrypt(sign);
        if(StringUtils.isEmpty(accountId)){
            return CommonResult.failed("传输密文错误,请重新输入");
        }
        if(!StringUtils.isEmpty(cid)) {
            int count = accountSetoutMapper.deleteByCid(cid);
            if (count > 0) {
                //成功后移除redis的数据
                redisService.remove(RedisKey.FAILED_DEL_SET_OUT +":"+cid);
                return CommonResult.success(null,"账户删除设备成功");
            }
        }
        return CommonResult.failed("删除设备失败，请检查信息");
    }
}
